<?php
class UploadService
{
	private $_uploadDir;
	private $_maxSize;
	private $_ext;	
	private $_upFileName;
	private $_smallFileName;
	
	function __construct($updir,$maxSize=null)
	{
		$this->_uploadDir	= $updir;
		$uploadConf	= C('LtConfig')->get('upload');
		if(is_null($maxSize) && array_key_exists('max_size', $uploadConf))
		{
			$maxSize	= $uploadConf['max_size'];
		}
		if(!is_null($maxSize))
		{
			$this->_maxSize		= $maxSize;
		}
		
		if(array_key_exists('ext', $uploadConf))
		{
			$this->_ext		= $uploadConf['ext'];
		}
	}
	
	public function upload($context,$uploadName,$fileName='',$small=0, $smallConf=array())
	{
		$uploadInfo		= $context->file($uploadName);
		$uploadFile 	= $this->_isUploadedFile($uploadInfo['tmp_name']) && $uploadInfo['tmp_name'] != 'none' && $uploadInfo['tmp_name'] && trim($uploadInfo['name']);
		if(!$uploadFile)
		{
			@unlink($uploadInfo['tmp_name']);
			return 1;
		}
		
		$fileExt = strtolower(Util::getFileExt($uploadInfo['name']));
		if(is_array($this->_ext) && !in_array($fileExt, $this->_ext))
		{
			@unlink($uploadInfo['tmp_name']);
			return 2;
		}
		
		if(isset($this->_maxSize) && filesize($uploadInfo['tmp_name'])>$this->_maxSize)
		{
			@unlink($uploadInfo['tmp_name']);
			return 3;
		}
		
		if($fileName=='')
		{
			$fileName	= Util::random(8).'.'.$fileExt;
		}
		else
		{
			$fileName	= $fileName.'.'.$fileExt;
		}
		$fileName	= date('ymd').'/'.$fileName;
		
		$upFile		= $this->_uploadDir.$fileName;
		$d			= dirname($upFile);
		if(!is_dir($d))
		{
			mkdir($d,0777,true);
		}
		
		if($small==0 || $small==1)
		{
			if(!@copy($uploadInfo['tmp_name'], $upFile))
			{
				if(!@move_uploaded_file($uploadInfo['tmp_name'], $upFile))
				{
					@unlink($uploadInfo['tmp_name']);
					return 4;
				}
			}
			else
			{
				@unlink($uploadInfo['tmp_name']);
			}
		}
		if($small==1 || $small==2)
		{
			if(array_key_exists('width', $smallConf) && is_numeric($smallConf['width']) && $smallConf['width']>0)
			{
				$width	= $smallConf['width'];
			}
			else
			{
				$width	= array_key_exists('width', $uploadConf['image']['small'])?$uploadConf['image']['small']['width']:100;
			}
			if(array_key_exists('height', $smallConf) && is_numeric($smallConf['height']) && $smallConf['height']>0)
			{
				$height	= $smallConf['height'];
			}
			else
			{
				$height	= array_key_exists('height', $uploadConf['image']['small'])?$uploadConf['image']['small']['height']:100;
			}
			if(array_key_exists('name', $smallConf) && $smallConf['name']!='')
			{
				$smallName	= $smallConf['name'];
			}
			else
			{
				$smallName	= $fileName.'.s.'.$fileExt;
			}
			$sFile			= $this->_uploadDir.$smallName;
			if($small==2)
			{
				$upFile		= $uploadInfo['tmp_name'];
			}
			$this->imageResize($upFile, $sFile, $width, $height);
			$this->_smallFileName = $smallName;
		}
		$this->_upFileName = $fileName;
		return 0;
	}
	
	public function imageResize($srcFile,$toFile,$toW,$toH) 
	{ 
		if($toFile=="")
		{ 
			$toFile = $srcFile;
		} 
		$info = ""; 
		$data = GetImageSize($srcFile,$info); 
		switch ($data[2]) 
		{ 
			case 1: 
				if(!function_exists("imagecreatefromgif"))
				{ 
					exit(); 
				} 
				$im 		= ImageCreateFromGIF($srcFile); 
				$bgcolor	= ImageColorAllocate($im,0,0,0); 
				$bgcolor 	= ImageColorTransparent($im,$bgcolor) ; 
				break; 
			case 2: 
				if(!function_exists("imagecreatefromjpeg"))
				{ 
					exit(); 
				} 
				$im 	= ImageCreateFromJpeg($srcFile); 
				break; 
			case 3: 
				$im 	= ImageCreateFromPNG($srcFile); 
			break; 
		} 
		$srcW	= ImageSX($im); 
		$srcH	= ImageSY($im); 
		if($srcw<=$toW && $srcH<=$toH) 
		{ 
			if($srcFile!=$toFile) 
			{ 
				copy($srcFile,$toFile);
			} 
			return; 
		} 
		$toWH	= $toW/$toH; 
		$srcWH	= $srcW/$srcH; 
		if($toWH<=$srcWH)
		{ 
			$ftoW	= $toW; 
			$ftoH	= $ftoW*($srcH/$srcW); 
		} 
		else
		{ 
			$ftoH	= $toH; 
			$ftoW	= $ftoH*($srcW/$srcH); 
		} 
		if($srcW>$toW||$srcH>$toH) 
		{ 
			if(function_exists("imagecreatetruecolor"))
			{ 
				@$ni 	= ImageCreateTrueColor($ftoW,$ftoH); 
				if($ni)
				{
					ImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
				}
				else
				{ 
					$ni=ImageCreate($ftoW,$ftoH); 
					ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); 
				} 
			}
			else
			{ 
				$ni	= ImageCreate($ftoW,$ftoH); 
				ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); 
			} 
			switch ($data[2]) 
			{ 
				case 1: 
					$bgcolor	= ImageColorAllocate($ni,0,0,0); 
					$bgcolor 	= ImageColorTransparent($ni,$bgcolor) ; 
					ImageGif($ni,$toFile); 
					break; 
				case 2: 
					ImageJpeg($ni,$toFile); 
					break; 
				case 3: 
					ImagePNG($ni,$toFile); 
					break; 
			}
			
			ImageDestroy($ni); 
		} 
		ImageDestroy($im); 
	}
	
	public function getUploadFileName()
	{
		return $this->_upFileName;
	}
	
	public function getSmallFileName()
	{
		return $this->_smallFileName;
	}
	
	public function _isUploadedFile($file)
	{
		return function_exists('is_uploaded_file') && (is_uploaded_file($file) || is_uploaded_file(str_replace('\\\\', '\\', $file)));
	}
}

?>